<?php
/**
 * Created by Dumitru Russu
 * User: Dumitru Russu
 * Date: 8/22/12
 * Time: 3:32 PM
 * To change this template use File | Settings | File Templates.
 */
class Page_Be_Cms_Skins extends Page
{
    /**
     * @var Db_Cms_Skins
     */
    protected $tableObject;

    function __construct() {
        $skinId = (int)str_replace('sk_key_', '', post('id'));
        $this->idTableObject = (!empty($skinId) ? $skinId : get('id', 'int'));
        $this->actionsTableObject = array(
            'saveDefaultAction',
            'saveCellAction',
            'saveAction',
            'configAction',
            'addEditSkinFileAction',
            'getSkinNameAction'
        );
        $this->classTableObject = 'Db_Cms_Skins';

        self::$TPL->appendJsFile('admin/skins');
        self::$TPL->appendCssFile('admin/skins');
        parent::__construct();
    }

    public function defaultAction() {
        if (perm('SKINS_ADD')) {
            $this->addBreadcrumb(t(T_FORM_BUTTON, 'add'), '?action=add');
        }
        $allSkins = Db_Cms_Skins::getAllSkins();

        self::$TPL->assign('allSkins', $allSkins);
        self::$TPL->setBodyTemplate('list_skins');

        return array(
            'type' => 'display'
        );
    }

    public function addAction() {
		perm('SKINS_ADD', '/cms/skins');
        $this->addBreadcrumb(t(T_FORM_BUTTON, 'add'), '?action=add', true);
        $allProjects = Db_Projects::getDistinctProjects();

        self::$TPL->assign('allProjects', $allProjects);
        self::$TPL->setBodyTemplate('add_skin');
        return array(
            'type' => 'display'
        );
    }

    public function saveDefaultAction() {
		perm('SKINS_ADD', '/cms/skins');
        $defaultSkins = post('default_skins', 'array');

		$this->message = t(T_FORM_MSG, 'success');

        foreach($defaultSkins as $key=>$value) {
            $this->tableObject->removeDefaultSkin($key);
            $this->tableObject->id = $value;
            $this->tableObject->sk_is_default = 1;
            if(!$this->tableObject->save(true)) {
                $this->error = true;
                $this->message = t(T_FORM_ERROR, 'unknown_error_occurred');
            }
        }
        return array(
            'type' => 'redirect',
            'query' => false
        );
    }

    public function saveCellAction() {
		perm('SKINS_ADD', '/cms/skins');
        if(!empty($this->idTableObject)) {
            $skinKey = post('value', 'string');
        } else {
            $skinId = (int)str_replace('sk_name_', '', post('id'));
            $skinName = strtolower(post('value', 'string'));
        }

        if(!empty($this->idTableObject) && !empty($skinKey)) {
            $this->tableObject->sk_key = $skinKey;

            if(!$this->tableObject->save()) {
                return array(
                    'type' => 'string',
                    'content' => $this->tableObject->objectArray->sk_key
                );
            } else {
                return array(
                    'type' => 'string',
                    'content' => $skinKey
                );
            }

        } else if (!empty($skinId) && !empty($skinName)) {
            $this->tableObject->id = $skinId;
            $this->tableObject->sk_name = strtolower($skinName);
            if(!$this->tableObject->save()) {
                return array(
                    'type' => 'string',
                    'content' => t(T_SKIN_TITLE, $this->tableObject->getSkinName($skinId))
                );
            } else {
                return array(
                    'type' => 'string',
                    'content' => t(T_SKIN_TITLE, $skinName)
                );
            }
        } else {
            return false;
        }
    }

    public function saveAction() {
		perm('SKINS_ADD', self::$PAGE['p_url']);
        $skinProjectIdentifier = post('sk_project_identifier','string');
        $skinName = strtolower(post('sk_name', 'string'));
        $skinDefault = strtolower(post('sk_default', 'string'));
        $skinKey = strtolower(post('sk_key', 'string'));
        if(!empty($skinProjectIdentifier) && !empty($skinName) && !empty($skinKey)) {
            if($skinDefault == 1) {
                $this->tableObject->removeDefaultSkin($skinProjectIdentifier);
                $this->tableObject->sk_is_default = $skinDefault;
            }
            $this->tableObject->sk_project_identifier = $skinProjectIdentifier;
            $this->tableObject->sk_key = $skinKey;
            $this->tableObject->sk_name = $skinName;
            $this->tableObject->deleted = 0;
            $this->message = t(T_FORM_MSG, 'success');
            if(!$this->tableObject->save(true)) {
                $this->error = true;
                $this->message = t(T_FORM_ERROR, 'unknown_error_occurred');
            }

            return array(
                'type' => 'redirect',
                'query' => false
            );

        } else {
            return array(
                'type' => 'redirect',
                'query' => false
            );
        }
    }

    public function deleteAction() {
		perm('SKINS_ADD', self::$PAGE['p_url']);
        $this->message = t(T_FORM_MSG, 'success');
        if(!Db_Cms_Skins::abstractDelete($this->idTableObject)) {
            $this->error = true;
            $this->message = t(T_FORM_ERROR, 'unknown_error_occurred');
        } else {
			Db_Cms_SkinFiles::abstractDeleteByField('sf_skin_id', $this->idTableObject);
		}
        return array(
            'type' => 'redirect',
            'query' => false
        );
    }

    public function getSkinNameAction() {
        $skinId = (int)str_replace('sk_name_', '', get('id'));
        return array(
            'type' => 'string',
            'content' => $this->tableObject->getSkinName($skinId)
        );
    }

    public function configAction() {
		perm('SKIN_FILES_VIEW', self::$PAGE['p_url']);
		$this->addBreadcrumb(t(T_PAGE_TITLE, 'config') . ' &quot;' . t(T_SKIN_TITLE, $this->tableObject->objectArray->sk_name ) . '&quot;', '?action=config&id=' . $this->idTableObject, true);
		if (perm('SKIN_FILES_EDIT')) {
			$this->addBreadcrumb(t(T_FORM_BUTTON, 'add'), '?action=addEditSkinFile&id='. $this->idTableObject);
		}
		$skinFiles = Db_Cms_SkinFiles::getAllFilesBySkinId($this->idTableObject);
		self::$TPL->assign('skinFiles', $skinFiles);
		self::$TPL->assign('skinId', $this->idTableObject);
		self::$TPL->setBodyTemplate('list_skin_files');
		return array(
			'type' => 'display'
		);
    }

    public function addEditSkinFileAction() {
        perm('SKIN_FILES_EDIT', self::$PAGE['p_url']);
		$this->addBreadcrumb(t(T_PAGE_TITLE, 'config') . ' &quot;' . t(T_SKIN_TITLE, $this->tableObject->objectArray->sk_name ) . '&quot;', '?action=config&id=' . $this->idTableObject);
		$skinFileId = get('fileId', 'int');
		$editSkinFile = new Db_Cms_SkinFiles($skinFileId);
		if(!empty($editSkinFile->objectArray->id)) {
			$skinFile = $editSkinFile->findArrayById($skinFileId);

			if($editSkinFile->objectArray->sf_special_content == 1) {
				$skinFileName = t(T_FORM_LABEL, 'special_content');
			} else {
				$skinFileName =  $editSkinFile->objectArray->sf_file_name;
			}

			$this->addBreadcrumb(t(T_FORM_BUTTON, 'edit') . ' &quot;' . $skinFileName . '&quot;' , '?action=addEditSkinFile&id=' . get('id', 'int') . '&fileId=' . $skinFileId, true);
		} else {
			$this->addBreadcrumb(t(T_FORM_BUTTON, 'add'), '?action=addEditSkinFile&id='. $this->idTableObject, true);
		}
		$enumVar = Db_Cms_SkinFiles::getEnumValues('sf_media');
		self::$TPL->assign('skinFileMedia', $enumVar);
		self::$TPL->assign('editSkinFile', $skinFile);
		self::$TPL->setBodyTemplate('add_edit_skin_file');
		return array(
			'type' => 'display'
		);
    }

    public function saveSkinFileAction() {
		perm('SKIN_FILES_EDIT', self::$PAGE['p_url']);
        $skinFileId = get('fileId', 'int');
        $skinFileType = strtolower(post('sf_type', 'string'));
        $skinFileSpecialContent = post('sf_special_content', 'int');
        $this->message = t(T_FORM_MSG, 'success');

        if($skinFileSpecialContent == 1) {
            $skinFileName = post('sf_name', 'string', true);
		} else {
			$skinFileName = post('sf_name', 'string');
		}
		$skinFiles = new Db_Cms_SkinFiles($skinFileId);
		if(!empty($skinFileName) && !empty($skinFileType)) {

            $skinFiles->sf_file_name = $skinFileName;
            $skinFiles->sf_file_type = $skinFileType;
            $skinFiles->sf_special_content = $skinFileSpecialContent;
            $skinFiles->sf_media = post('sf_media', 'string');
            $skinFiles->sf_is_static = post('sf_static', 'int');

            if(empty($skinFileId)) {
                $skinFiles->sf_skin_id = $this->idTableObject;
            }

            if(!$skinFiles->save()) {
                $this->error = true;
                $this->message = t(T_FORM_ERROR, 'unknown_error_occurred');
            } else {
                $skinFilesNew = new Db_Cms_SkinFiles($skinFiles->id);
                if($skinFiles->objectArray->sf_file_type != $skinFileType) {
                    $skinFilesNew->positionCommonFields = array(
                        'sf_skin_id' => $skinFilesNew->objectArray->sf_skin_id,
                        'sf_file_type' => $skinFilesNew->objectArray->sf_file_type
                    );
                    if(!$skinFilesNew->positionSetLast()) {
                        $this->error = true;
                        $this->message = t(T_FORM_ERROR, 'unknown_error_occurred');
                    }
                    if($skinFiles->objectArray->sf_file_type) {
                        $skinFilesNew->positionCommonFields['sf_file_type'] = $skinFiles->objectArray->sf_file_type;
                        $skinFilesNew->positionCleanUp();
                    }
                }
            }
        } else {
            $this->message = t(T_FORM_ERROR, 'unknown_error_occurred');
        }
        if(!empty($skinFileId)) {
            return array(
                'type' => 'redirect',
                'content' => '?action=config&id='. $skinFiles->objectArray->sf_skin_id
            );
        } else {
            return array(
                'type' => 'redirect',
                'content' => '?action=config&id='.$this->idTableObject
            );
        }
    }

    public function deleteSkinFileAction() {
		perm('SKIN_FILES_EDIT', self::$PAGE['p_url']);
        $skinId = get('skinId', 'int');
        $skinFileId = get('id', 'int');
        $skinFiles = new Db_Cms_SkinFiles($skinFileId);
        $skinFiles->positionCommonFields = array(
            'sf_skin_id' => $skinFiles->objectArray->sf_skin_id,
            'sf_file_type' => $skinFiles->objectArray->sf_file_type
        );
        $this->message = t(T_FORM_MSG, 'success');

        if(!Db_Cms_SkinFiles::abstractDelete($skinFileId) || !$skinFiles->positionCleanUp()) {
            $this->error = true;
            $this->message = t(T_FORM_ERROR, 'unknown_error_occurred');
        }

        return array(
            'type' => 'redirect',
            'content' => '?action=config&id=' . $skinId
        );
    }

    public function saveSkinFilesPositionAction() {
        $currentId = post('currentId', 'int');
        $btnAction = post('btnAction', 'string');
        $skinFiles  = new Db_Cms_SkinFiles($currentId);
        $this->error = true;
        $this->message = t(T_FORM_MSG, 'set_move_action');
        $skinFiles->positionCommonFields = array(
            'sf_skin_id' => $skinFiles->objectArray->sf_skin_id,
            'sf_file_type' => $skinFiles->objectArray->sf_file_type
        );

        if($btnAction == 'up') {
            if(!$skinFiles->positionMoveUp()) {
                $this->message = t(T_FORM_MSG, 'move_up_msg_false');
                return array(
                    'type' => 'encoded_json',
                    'content' => $this->jsonResult($this->error, $this->message, !$this->error)
                );
            }
        } else if($btnAction == 'down') {
            if(!$skinFiles->positionMoveDown()) {
                $this->message = t(T_FORM_MSG, 'move_down_msg_false');
                return array(
                    'type' => 'encoded_json',
                    'content' => $this->jsonResult($this->error, $this->message, !$this->error)
                );
            }
        } else {
            return array(
                'type' => 'encoded_json',
                'content' => $this->jsonResult($this->error, $this->message, !$this->error)
            );
        }
        return false;
    }
}
